package com.example.yiqiuser.service.impl;


import com.example.yiqiuser.dao.MenuDao;
import com.example.yiqiuser.entity.TreeNode;
import com.example.yiqiuser.service.MenuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ fileName:MenuServiceImpl
 * @ description:
 * @ author:xjd
 * @ createTime:2023/3/27 11:22
 * @ version:1.0.0
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Resource
    private MenuDao menuDao;

    @Override
    public List<TreeNode> queryTreeData() {
        //查询所有数据
        List<TreeNode> treeNodeList = menuDao.queryAllTree();
        //System.out.println("获取menu所有数据："+treeNodeList);
        //定义返回集合
        List<TreeNode> returnTreeNodeList = new ArrayList<>();
        //判断所有数据是否为null
        //if(treeNodeList!=null&&treeNodeList.size()>0){
            if (!CollectionUtils.isEmpty(treeNodeList)){
                //循环遍历，查找一级节点
                for (TreeNode treeNode : treeNodeList) {
                    //父节点为0，都是根节点
                    if (treeNode.getParentId()==0){
                        returnTreeNodeList.add(treeNode);
                        //为1级节点找子节点
                        bindChildren(treeNode,treeNodeList);
                    }
                }
            }
        return returnTreeNodeList;
    }

    @Override
    public List<TreeNode> queryByUserId(Integer userId) {
        //查询所有数据
        List<TreeNode> treeNodeList = menuDao.queryByUserId(userId);
        //System.out.println("获取menu所有数据："+treeNodeList);
        //定义返回集合
        List<TreeNode> returnTreeNodeList = new ArrayList<>();
        //判断所有数据是否为null
        //if(treeNodeList!=null&&treeNodeList.size()>0){
        if (!CollectionUtils.isEmpty(treeNodeList)){
            //循环遍历，查找一级节点
            for (TreeNode treeNode : treeNodeList) {
                //父节点为0，都是根节点
                if (treeNode.getParentId()==0){
                    returnTreeNodeList.add(treeNode);
                    //为1级节点找子节点
                    bindChildren(treeNode,treeNodeList);
                }
            }
        }
        return returnTreeNodeList;
    }


    /**
     * 从所有节点中查找当前节点的孩子
     * @param currentTreeNode
     * @param treeNodeList
     */
    private void bindChildren(TreeNode currentTreeNode,List<TreeNode> treeNodeList){
        //循环查找当前节点的孩子
        for (TreeNode treeNode : treeNodeList) {
            //判断循环节点的父节点id是否和当前节点id相等
            if (treeNode.getParentId()==currentTreeNode.getId()){
                //获取当前节点的孩子集合
                List<TreeNode> children = currentTreeNode.getChildren();
                //判断孩子集合是否为null
                if (children==null){
                    //实例化对象，否则会报空指针异常(当前父节点找第一个孩子节点的时候集合为空，所有要实例化一个集合对象)
                    children = new ArrayList<>();
                }
                //添加当前节点到孩子集合
                children.add(treeNode);
                //和当前对象进行绑定(覆盖当前对象children集合属性)
                currentTreeNode.setChildren(children);
                //递归 调用自己为当前节点绑定孩子(传入当前循环节点)
                bindChildren(treeNode,treeNodeList);
            }
        }
    }



    @Override
    public int add(TreeNode treeNode) {
        try {
            menuDao.add(treeNode);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(TreeNode treeNode) {
        try {
            menuDao.update(treeNode);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteById(int id) {
        return menuDao.deleteById(id);
    }
}
